package org.NooLab.itexx.app.gui;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.NooLab.itexx.app.gui.definition.ITexxWindowGui;
import org.NooLab.itexx.app.gui.definition.ITexxWindowGuiIntf;
import org.NooLab.itexx.app.gui.handlers.PanelText;
import org.NooLab.utilities.net.GUID;
import org.NooLab.utilities.strings.StringsUtil;



/**
 * 
 * 

mouse over -> activates for keyboard commands

			  if activated, and pressed some key do this or that


double click to pane -> open source

click to any pane -> enable clipboard for 20 seconds

possibility to sort for relevancy
such that the most relevant is on top


sp fields:

 *
 */
public class StackedPanel {

	transient StackedPanelProducer panelProducer;
	transient StackedPanels parent ;
	transient ITexxWindowGuiIntf windowGui ;
	transient JPanel hostingPanel;
	
	static StackedPanel stackedPanel;
	
	CombiPanel combiPanel;
	// PanelText ptext;
	
	String imgFilePath = "";
	
	int index = 0;
	String guidStr = "" ;

	/** on mouse in/out  */
	boolean activated ;
	/** regarding current configuration */
	double relevancy ; 

	/** a simple pointer for use by external contexts... */
	String referenceDataGuid = "";
	long   referenceDataId = -1L;
	
	

	public StringsUtil strgutil;
	// ------------------------------------------------------------------------	
	public StackedPanel( StackedPanelProducer panelProducer, StackedPanels parent ) throws IOException{
		
		this.parent = parent;
		this.panelProducer = panelProducer;
		
		strgutil = panelProducer.strgutil ;
		
		guidStr = GUID.randomvalue() ;
	
		windowGui = parent.appCommons.getWindowGui() ; 
		
		hostingPanel = windowGui.getjPanelScrollWrapFoundStack();
	
		stackedPanel = this;
		
		index = parent.size();
		create();
	}
	// ------------------------------------------------------------------------
	
	private void create() throws IOException{

		combiPanel = null;
		String imgResource ="org/NooLab/itexx/app/gui/resources/button-glassy background out.png" ;
		org.NooLab.utilities.resources.ResourceLoader rL = new org.NooLab.utilities.resources.ResourceLoader();
		
		boolean hb = rL.loadImageResource( this.getClass(), imgResource) ;
		if (hb){
			Image img=null;
			img = rL.getImageResource() ;
			combiPanel = new CombiPanel( this,img) ;
		}else{
			combiPanel = new CombiPanel( this ) ;
		}
		
		int h,w;

		Dimension ds = windowGui.getjPanelFoundScrollerHost().getSize();
		
		
		windowGui.getScrollPaneReturnedResults().setSize( ds);
		windowGui.getScrollPaneReturnedResults().setPreferredSize( ds);

		
		h = combiPanel.getHeight() *(index+1) ;
		Dimension d = new Dimension( 200,h);
		windowGui.getjPanelScrollWrapFoundStack().setPreferredSize( d )  ;
		
		h = windowGui.getjPanelScrollWrapFoundStack().getHeight()  ;
		w = windowGui.getjPanelScrollWrapFoundStack().getWidth() ;
		
		windowGui.getjPanelScrollWrapFoundStack().add( combiPanel );

		combiPanel.getPanelText();
	}
	
	
	public void update() {
		// 
		combiPanel.updateText() ;
	}

	public void clear(){
		combiPanel.setToolTipText( "") ;
		combiPanel.setHtmlText( "" );
	}
	
	public void setText(String string) {
		// jsut displaying
		combiPanel.getPanelText().setText(string) ;
		return ;
	}

	public void setPanelText(String panelText) {
		// includes css handling !!
		// is CSS available?
		
		// then displaying the html on the EditorPane
		combiPanel.setHtmlText( "" );
		combiPanel.setHtmlText( panelText ) ;
		combiPanel.repaint() ;
	}


	public void setPanelText(PanelText panelText) {
		// 
		combiPanel.setPanelText(panelText) ;
	}

	public PanelText getPanelText() {
		// 
		return combiPanel.getPanelText();
	}

	
	public void setTooltipText(String urlStr) {
		// 
		combiPanel.setToolTipText( urlStr) ;
	}

	public void insertImage( String imgFilePath, int external) throws IOException{

		this.imgFilePath = imgFilePath;
		
		BufferedImage myPicture = ImageIO.read(new File("path-to-file"));
		JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
		// add( picLabel );

	}

	public String getGuid() {
		// 
		
		return guidStr;
	}

	public ITexxWindowGuiIntf getWindowGui() {
		return windowGui;
	}


	public boolean isActivated() {
		return activated;
	}

	public void setActivated(boolean activated) {
		this.activated = activated;
	}

	public double getRelevancy() {
		return relevancy;
	}

	public void setRelevancy(double relevancy) {
		this.relevancy = relevancy;
	}

	public StackedPanelProducer getPanelProducer() {
		return panelProducer;
	}

	public void openDocumentRequest() {
		// through external program
		
	}

	public static void clearClipboardChannel(int duration) {
		new ClearClipBoard(stackedPanel);
	}
	
	public void openClipboardChannel(int duration) {
		// 
		panelProducer.appCommons.activateClipboard( duration ) ;
	}

	public void repaint() {
		//
		combiPanel.repaint();
	}


}

class ClearClipBoard{
	public ClearClipBoard(StackedPanel stackedPanel){
		stackedPanel.panelProducer.appCommons.activateClipboard( -11 ) ;	
	}
	
}











